//
//  RepeatDateController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/19.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "RepeatDateController.h"
#import "RepeatDateCell.h"
#import "RepeatDateModel.h"
#import "YHMarcros.h"
@interface RepeatDateController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray<RepeatDateModel *> *dates;
@end

@implementation RepeatDateController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"重复日期";
    NSArray *titles = @[@"每周一",@"每周二",@"每周三",@"每周四",@"每周五",@"每周六",@"每周日"];
    NSMutableArray *arryM = [NSMutableArray arrayWithCapacity:titles.count];
    uint8_t index = 1;
    for (NSString * title in titles) {
        RepeatDateModel *model = [[RepeatDateModel alloc]init];
        model.title = title;
        model.selected = NO;
        model.wday =index;
        [arryM addObject:model];
        index++;
        
    }
    self.dates = [arryM copy];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(10);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dates.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RepeatDateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.repeatDate = self.dates[indexPath.row];

    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.dates[indexPath.row].selected = !self.dates[indexPath.row].selected;
    
    NSMutableArray *selectArry = [NSMutableArray array];
    for (RepeatDateModel *repeatDate in self.dates) {
        if (repeatDate.selected) {
            [selectArry addObject:repeatDate];
        }
    }
    if (self.repeatDateBlock) {
        self.repeatDateBlock(selectArry);
    }
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kBgColor;
        _tableView.rowHeight = 55;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerClass:[RepeatDateCell class] forCellReuseIdentifier:@"cell"];
        _tableView.bounces = NO;
    }
    return _tableView;
}



@end
